package com.jk1123.reference.weak;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2021-08-20 19:12
 * @description
 *
 * 弱引用 垃圾回收   就把数据回收 了
 *
 */
public class WeakTest {
    public static void main(String[] args) throws InterruptedException {

        List<WeakReference> list=new ArrayList();
        for (int i = 0; i < 10; i++) {

            byte[] buff=new byte[1024*1024];
            WeakReference<byte[]> weakReference = new WeakReference<>(buff);
            list.add(weakReference);
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(list.get(i).get());
        }
        System.gc();
        //垃圾回收后  弱引用无论如何都会回收掉
        for (int i = 0; i < 10; i++) {
            System.out.println(list.get(i).get());
        }


        ThreadLocal threadLocal=new ThreadLocal();

        threadLocal.set("123123");


    }
}
